<!-- 活动新闻分类 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between">
                    <a-col :md="6">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="" icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" @change="handleTableChange" :columns="columns" :data-source="dataList"  size="small" >
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <!-- 添加、修改、删除新闻分类 -->
        <a-modal
            title="添加、修改新闻分类"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="分类ID">
                        <a-tag color="#f50">
                            添加时，如果新添加的ID已存在，则会替换掉当前存在的分类信息
                        </a-tag>
                        <a-input v-model="item.appInfoType" type="number" :disabled="!isAdd" placeholder="请输入分类ID" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="分类名称">
                        <a-input v-model="item.description" placeholder="请输入分类名称" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { updateNewsClassification , queryNewsClassification } from '@/api/brand';
export default {
    data () {
        return {
            columns,
            dataList: [],
            brandId: this.global.brandId,
            brandList: [],
            page: 1,
            size: 10,
            total: 0,
            isload: false,
            item: {
                appInfoType: '',
                description: '',
            },
            visible: false,
            confirmLoading: false,
            isAdd: true,
        };
    },
    created(){
        this.getData()
    },
    methods: {
        // 分页查询
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        getData(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryNewsClassification(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data
                    _this.isload = false
                    console.log(_this.dataList,'dataList')
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 添加
        add(){
            if(!this.brandId){
                this.$message.warning('请先选择品牌！')
                return
            }
            this.item.brandId = this.brandId
            this.item.appInfoType = this.dataList.length
            this.item.description = ''
            this.isAdd = true
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.isAdd = false
            this.visible = true
        },
        // 确定添加、修改
        handleOk(){
            let _this = this
            if(_this.item.description == ''){
                _this.$message.warning('请输入新闻分类名称');
                return
            }
            const params = {
                'brandId':_this.item.brandId,
                'appInfoType':_this.item.appInfoType,
                'description':_this.item.description,
            }
            if(_this.isAdd && _this.dataList.filter((item)=>{return item.appInfoType == _this.item.appInfoType}).length > 0){
                
                    this.$confirm({
                        title: '温馨提示',
                        content: '当前分类ID已经存在，是否替换掉已存在的分类信息？',
                        onOk() {
                            updateNewsClassification(params)
                            .then(res=>{
                                if(res.success){
                                    _this.$notification['success']({
                                        message: '提示',
                                        description: _this.isAdd ? '添加成功' : '修改成功',
                                        duration: 8,
                                    })
                                    _this.getData()
                                    _this.visible = false
                                }else{
                                    _this.$notification['error']({
                                        message: '提示',
                                        description: res.errMessage,
                                        duration: 8,
                                    })
                                }
                            })
                        },
                        onCancel() {},
                    });
            }else{
                updateNewsClassification(params)
                .then(res=>{
                    if(res.success){
                        _this.$notification['success']({
                            message: '提示',
                            description: _this.isAdd ? '添加成功' : '修改成功',
                            duration: 8,
                        })
                        _this.getData()
                        _this.visible = false
                    }else{
                        _this.$notification['error']({
                            message: '提示',
                            description: res.errMessage,
                            duration: 8,
                        })
                    }
                })
            }
        },
        // 删除分类信息
        del(item){
            let _this = this
            const params = {
                'brandId':item.brandId,
                'appInfoType':item.appInfoType,
                'isDelete':1,
            }
            updateNewsClassification(params)
            .then(res=>{
                if(res.success){
                    this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    this.getData()
                }
            })
        },
    }
}

</script>
<style scoped>

</style>